package text;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;



public class userSystem {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        loop:
        while (true) {
            System.out.println("---------      学生管理系统      ---------");
            System.out.println("---------1.登录           2.注册---------");
            System.out.println("---------3.忘记密码             ---------");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的选择");
            String choose = sc.next();
            switch (choose) {
                case "1" -> login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                default -> System.out.println("没有该选项,请重新选择");
            }
        }
    }

    public static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        int count = 3;
        while (count-- != 0) {
            System.out.println("请输入用户名");
            String userName = sc.next();
            int index = getIndex(list, userName);
            if (index == -1) {
                System.out.println("用户未注册, 请先注册");
                return;
            }
            System.out.println("请输入用户密码");
            String password = sc.next();
            while (true) {
                String captcha1 = getCaptcha();
                System.out.println("验证码: " + captcha1);
                System.out.println("请输入验证码");
                String captcha = sc.next();
                if (captcha.equalsIgnoreCase(captcha1)) {
                    break;
                } else {
                    System.out.println("验证码错误, 请重新输入");
                }
            }
            if (list.get(index).getUsername().equals(userName) && list.get(index).getPassword().equals(password)) {
                System.out.println("登陆中.");
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("登陆中..");
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("登陆中...");
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("登陆中.");
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("登陆中..");
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("登陆中...");
                try {
                    Thread.sleep(200);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("登陆成功!");
                return;
            }
            if (count != 0) {
                System.out.println("登陆失败, 用户名或密码错误, 请重新输入");
            } else {
                System.out.println("登陆失败,用户名或密码错误, 账户已被锁定, 请联系管理员");
            }
        }
    }

    public static void register(ArrayList<User> list) {
        User u = new User("zhangsan", "123");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入用户名");
            String userName = sc.next();
            if (checkuserName(list, userName)) {
                u.setUsername(userName);
                break;
            }
        }
        while (true) {
            System.out.println("请输入密码");
            String password1 = sc.next();
            System.out.println("请再次输入密码");
            String password2 = sc.next();
            if (password1.equals(password2)) {
                u.setPassword(password2);
                break;
            } else {
                System.out.println("密码不一致,请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入身份证号码");
            String id = sc.next();
            if (checkid(id)) {
                u.setId(id);
                break;
            }
        }
        while (true) {
            System.out.println("请输入手机号码");
            String phoneNumber = sc.next();
            if (checkphoneNumber(phoneNumber)) {
                u.setPhonenumber(phoneNumber);
                break;
            }
        }
        list.add(u);
        System.out.println("注册成功!");
    }

    public static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String userName = sc.next();
        int index = getIndex(list, userName);
        if (index == -1) {
            System.out.println("未注册");
            return;
        }
        System.out.println("请输入省份证号码");
        String id = sc.next();
        System.out.println("请输入手机号码");
        String phonenumber = sc.next();
        if (list.get(index).getId().equals(id) && list.get(index).getPhonenumber().equals(phonenumber)) {
            while (true) {
                System.out.println("请输入密码");
                String password1 = sc.next();
                System.out.println("请再次输入密码");
                String password2 = sc.next();
                if (password1.equals(password2)) {
                    list.get(index).setPassword(password1);
                    System.out.println("修改成功!");
                    break;
                } else {
                    System.out.println("密码不一致,请重新输入");
                }
            }
        } else {
            System.out.println("账号信息不匹配,修改失败");
        }

    }

    public static String getCaptcha() {

        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; ) {
            int randomChar = r.nextInt(65, 123);
            if (randomChar >= 65 && randomChar <= 90 || randomChar >= 97 && randomChar <= 122) {
                sb.append((char) randomChar);
                i++;
            }

        }
        sb.append(r.nextInt(10));
        char[] arr = sb.toString().toCharArray();
        char temp = arr[arr.length - 1];
        int randomIndex = r.nextInt(arr.length);
        arr[arr.length - 1] = arr[randomIndex];
        arr[randomIndex] = temp;
        return new String(arr);
    }

    public static int getIndex(ArrayList<User> list, String userName) {

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(userName)) {
                return i;
            }
        }
        return -1;
    }


    public static boolean checkuserName(ArrayList<User> list, String userName) {
        if (getIndex(list, userName) != -1) {
            System.out.println("用户名已存在, 请重新输入");
            return false;
        }
        if (userName.length() < 3 || userName.length() > 15) {
            System.out.println("用户名非法, 用户名长度不能小于3和大于15");
            return false;
        }
        for (int i = 0; i < userName.length(); i++) {
            if (userName.charAt(i) < '0' || userName.charAt(i) > '9' && userName.charAt(i) < 'A' || userName.charAt(i) > 'Z' && userName.charAt(i) < 'a' || userName.charAt(i) > 'z') {
                System.out.println("用户名非法, 用户名是数字和字母组成");
                return false;
            }
            if (userName.charAt(i) >= 'A' && userName.charAt(i) <= 'Z' || userName.charAt(i) >= 'a' && userName.charAt(i) <= 'z') {
                return true;
            }
        }
        System.out.println("用户名非法, 用户名不能全为数字");
        return false;
    }

    public static boolean checkid(String id) {
        if (id.length() != 18) {
            System.out.println("身份证号码长度为18位");
            return false;
        }
        if (id.charAt(0) == '0') {
            System.out.println("省份证号码不能以0开头");
            return false;
        }
        for (int i = 0; i < id.length() - 1; i++) {
            if (id.charAt(i) < '0' || id.charAt(i) > '9') {
                System.out.println("省份证号码必须是数字");
                return false;
            }
        }
        if (id.charAt(17) < '0' || id.charAt(17) > '9' && id.charAt(17) != 'x' && id.charAt(17) != 'X') {
            System.out.println("身份证号码最后一位只能是数字或'x'或'X'");
            return false;
        }
        return true;
    }

    public static boolean checkphoneNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            System.out.println("手机号码必须是11位数");
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            if (phoneNumber.charAt(i) < '0' || phoneNumber.charAt(i) > '9') {
                System.out.println("手机号码必须数字");
                return false;
            }
        }
        if (phoneNumber.charAt(0) == '0') {
            System.out.println("手机号码不能以0开头");
            return false;
        }
        return true;
    }


}
